package com.qf.demo;

import java.sql.*;

/**
 * @version 1.0
 * 公众号：Java架构栈
 * @Author: 卓不凡
 */
public class Demo01 {
    public static void main(String[] args) throws Exception {
        //c3p0  druid  dbcp  hikiri
        //模拟别人修改完代码之后，提交到远程仓库
        //制造冲突远程仓库的
        Connection connection = null; //连接对象
        PreparedStatement preparedStatement = null; //执行对象
        ResultSet resultSet = null;//结果集对象
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");  //编译期不会报错 ，运行时报错
        //DriverManager.registerDriver(new com.mysql.jdbc.Driver());  //编译期错误
        //获得连接
        connection =
                DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "123");
        //定义sql
        String sql = "select * from dept where id = ?"; //？占位符，防止sql注入攻击
        //获取语句执行对象
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, 1);
        //执行sql得到结果集
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id:" + id + "name:" + name);
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
